package cn.iocoder.yudao.module.game.service.agentTeam;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.agentTeam.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.agentTeam.AgentTeamDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.agentTeam.AgentTeamMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 代理团队 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class AgentTeamServiceImpl implements AgentTeamService {

    @Resource
    private AgentTeamMapper agentTeamMapper;

    @Override
    public Long createAgentTeam(AgentTeamSaveReqVO createReqVO) {
        // 插入
        AgentTeamDO agentTeam = BeanUtils.toBean(createReqVO, AgentTeamDO.class);
        agentTeamMapper.insert(agentTeam);
        // 返回
        return agentTeam.getId();
    }

    @Override
    public void updateAgentTeam(AgentTeamSaveReqVO updateReqVO) {
        // 校验存在
        validateAgentTeamExists(updateReqVO.getId());
        // 更新
        AgentTeamDO updateObj = BeanUtils.toBean(updateReqVO, AgentTeamDO.class);
        agentTeamMapper.updateById(updateObj);
    }

    @Override
    public void deleteAgentTeam(Long id) {
        // 校验存在
        validateAgentTeamExists(id);
        // 删除
        agentTeamMapper.deleteById(id);
    }

    private void validateAgentTeamExists(Long id) {
        if (agentTeamMapper.selectById(id) == null) {
            throw exception(AGENT_TEAM_NOT_EXISTS);
        }
    }

    @Override
    public AgentTeamDO getAgentTeam(Long id) {
        return agentTeamMapper.selectById(id);
    }

    @Override
    public PageResult<AgentTeamDO> getAgentTeamPage(AgentTeamPageReqVO pageReqVO) {
        return agentTeamMapper.selectPage(pageReqVO);
    }

}